package com.zw.mizhe.view.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.zw.mizhe.R;
import com.zw.mizhe.bean.HomeBean;
import com.zw.mizhe.common.base.BaseLazyFragment;
import com.zw.mizhe.view.adapter.HomePagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class HomeFragment extends BaseLazyFragment {
    public static final String PARAMS_ARGS_TYPE = "type";
    @BindView(R.id.home_tabs)
    TabLayout homeTabs;
    @BindView(R.id.home_vp)
    ViewPager homeVp;
    //1 表示 特卖 2 表示 包邮 , 3表示的拼团
    private int type;

    private HomePagerAdapter pagerAdapter;

    List<HomeBean> homeData;
    private ArrayList<Fragment> fragments = new ArrayList<>();

    public static HomeFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt(PARAMS_ARGS_TYPE, type);
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void before() {
        if (getArguments() != null) {
            type = getArguments().getInt(PARAMS_ARGS_TYPE);
            homeData = HomeBean.getHomeData(type);
            for (int i = 0; i < homeData.size(); i++) {
                fragments.add(HomeChildFragment.newInstance(homeData.get(i).getRequestPath()
                        , homeData.get(i).getRequestType()));
            }
        }
    }

    @Override
    public void initView() {
        pagerAdapter = new HomePagerAdapter(getChildFragmentManager(), fragments, homeData);
        homeVp.setAdapter(pagerAdapter);
        if (type == 2) {
            homeTabs.setTabMode(TabLayout.MODE_FIXED);
        }
        homeTabs.setupWithViewPager(homeVp);
    }


    @Override
    public void initData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

}
